﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace Eudaemon.ProeAssist.WinGUIControls
{
    /// <summary>
    /// Only folder node supported.
    /// Based on "Professional C# 2005 (4th edition)" (chapter 23.4.3)
    /// Todo: Adds exceptions; Refresh folder tree; Load tree using additional worker thread.
    /// </summary>
    public class FolderTree : System.Windows.Forms.TreeView, ISupportInitialize
    {
        public FolderTree()
        {
        }

        //[Category("Behavior"), Description("Gets or sets the base or root folder of the tree"), DefaultValue("C:\\")]
        //public string RootFolder
        //{
        //    get
        //    {
        //        return _rootFolder;
        //    }
        //    set
        //    {
        //        _rootFolder = value;
        //        if (!_inInit)
        //            InitializeTree();
        //    }
        //}

        [Browsable(false)]
        public string SelectedFolder
        {
            get
            {
                if(this.SelectedNode is FolderNode)
                    return ((FolderNode)(this.SelectedNode)).FolderPath;
                return "";
            }
        }

        //public void Refresh()
        //{
        //}

        #region ISupportInitialize Members

        public void BeginInit()
        {
            //_inInit = true;
        }

        public void EndInit()
        {
            //if(_rootFolder != "")
                InitializeTree();
            //_inInit = false;
        }

        #endregion


        #region Private
        private void LoadTree(FolderNode folder)
        {
            string[] dirs = Directory.GetDirectories(folder.FolderPath);
            foreach (string dir in dirs)
            {
                FolderNode tmpFolder = new FolderNode(dir);
                folder.Nodes.Add(tmpFolder);
                ///LoadTree(tmpFolder);
            }
        }

        private void InitializeTree()
        {
            if (!this.DesignMode)
            {
                this.Nodes.Clear();
                DriveInfo[] allDrives = DriveInfo.GetDrives();
                string root;
                foreach (DriveInfo drive in allDrives)
                {
                    if (drive.IsReady)
                    {
                        root = drive.Name;
                        FolderNode rootNode = new FolderNode(root);
                        LoadTree(rootNode);
                        this.Nodes.Add(root);
                    }
                }                
            }
        }

        //string _rootFolder = "";
        //bool _inInit = false;
        #endregion

    }

    public class FolderNode : System.Windows.Forms.TreeNode
    {
        public FolderNode(string folderPath)
        {
            this.FolderPath = folderPath;
            _info = new DirectoryInfo(folderPath);
            this.Text = _info.Name;
        }

        public string FolderPath
        {
            get
            {
                return _folderPath;
            }
            set
            {
                _folderPath = value;
            }
        }

        public DirectoryInfo FolderNodeInfo
        {
            get
            {
                return _info;
            }
        }

        string _folderPath = "";
        DirectoryInfo _info;
    }

}
